<?php

namespace App\Http\Controllers;
use App\Http\Utils\kabao\AopClient;
use App\Http\Utils\kabao\request\AlipaySystemOauthTokenRequest;
use App\Http\Utils\kabao\request\AlipayMarketingCardOpenRequest;
use App\Http\Utils\kabao\request\AlipayMarketingCardQueryRequest;

class CardController extends Controller{
    private	$format = 'json';
    private	$aop;
    function __construct(){
        header("Content-type:text/html;charset=utf-8");
        $this->aop = new AopClient ();
        $this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $this->aop->appId = '2018011901971979';
        $this->aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEA0Hx+9wgN4ParmGATI2ykd4HnwqV2yhUKfPXCLWMXCxyT83akGBPt/svUBeYCd5ZhVMMebYQ6ZfJVtfCCF6aQAQQvZujgcCdWm4nO6W1w7ryIhuwvT1ocsBKBGl3g+9YfvHWfPx2otIpvL5xPBYz3+0bk0Ge5EuVT4xjNsfIfNVMhOCddtX0VOfNFE7gzu6uVsEatFXLcwq2qCTVrx4SWjLCpYVmTdtoFAgJqrSvuXHDccAj7lA44vy1xJRpfzhddE+uQ2G+wcoK6Z/CW6RDpJ1zLhfcSRfvxO+gWLk72UI7W6U3LAa5ULc3moUqFbOfjE73fjQIynCIoAT1AdRq7WwIDAQABAoIBAQCBiQid1dxBrSFakD7z4JmjQwxLbOEMl9wnXc6iwpFbrwrh1xIHf8J8cAoxDAL2aHHa50xtgR2Dn/jPDTrXFP8eEoqdJw3jwR1XkF0FvaDQREWThwWyCOwQHb8nMbMK5bRcNLyXy4a5fdWtQDH84eRltFuQfRGJgwEG9a8/446uYvP5ZuW5DDyGWKJ6XbQbaYfsZ7ID5ek4/I9xZYUeMRe8xyeGB2Es4Gq0Cz0q2uGicUOB2d28nPNnK9gzSrJIo3EZu9nJg+T83YSMeO5f0T3YW1Id2nvL4Ksbp6Gx23jLbGCIKcDKzJLgt/yJBEokYHaTzU1VN98yBbTor27XSik5AoGBAOvQ4LCNpOwJLVpX+JC/7yFz24XfnYy8gqwN4fVd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w/mvMpRFzYb/FlfUl72ewOr7FCRn3mBu5PYDNkrTQiNiECOXj1bO6/O6rFZ8lqvOV7Tb0hk8iobT19dMhlUCR4jE1jWGTTHVHZh1OrlV1zHjkHcRdLihCyMM3WQWz5pOvDuBhtl9JVA/MaeeFEgHTxde3QIDAQAB';
        $this->aop->apiVersion = '1.0';
        $this->aop->signType = 'RSA2';
        $this->aop->postCharset='utf-8';
        $this->aop->format='json';
    }
    public function get_token($code){
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $result = $this->aop->execute ( $request);
        return     [
            $result->alipay_system_oauth_token_response->access_token,                  $result->alipay_system_oauth_token_response->user_id
        ];
    }
    public function card_open($template_id,$token,$user_uni_id){
        $request = new AlipayMarketingCardOpenRequest ();
        $post_data=[
            'out_serial_no'=>'E'.date('YmdHis').mt_rand(10000,99999),
            'card_template_id'=>$template_id,
            'card_user_info'=>[
                'user_uni_id'=>$user_uni_id,
                'user_uni_id_type'=>'UID',
            ],
            'card_ext_info'=>[
                'biz_card_no' => 'hha'.mt_rand(10000,99999),
                'external_card_no'=>mt_rand(10000,99999),
                'open_date' => date('Y-m-d H:i:s'),
                'valid_date' => date('2022-m-d H:i:s'),
            ],
        ];
        $request->setBizContent(json_encode($post_data,JSON_UNESCAPED_UNICODE));

        $result = $this->aop->execute ( $request,$token  );
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode->card_info->biz_card_no;
    }
    public function Market_query($target_card_no){
        $request = new AlipayMarketingCardQueryRequest ();
        $post_data=[
            'target_card_no'=>$target_card_no,
            'target_card_no_type'=>'BIZ_CARD'
        ];
        $request->setBizContent(json_encode($post_data,JSON_UNESCAPED_UNICODE));
        $result = $this->aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode->schema_url;
    }
    public function get_cart(){

        $templateid='20180124000000000768303000300732';
        if (isset($_GET['auth_code'])){
            $code=$_GET['auth_code'];
            $get_token=$this->get_token($code);
            $token=$get_token[0];
            $user_id=$get_token[1];
            $biz_card_no=$this->card_open($templateid,$token,$user_id);
            $schema_url=$this->Market_query($biz_card_no);
            header("location:".$schema_url);
            exit;
        }
    }
}